//
//  UIViewController+JJ_BaseNav.m
//  Udeco
//
//  Created by MacPro on 2017/8/6.
//  Copyright © 2017年 MacPro. All rights reserved.
//

#import "UIViewController+JJBaseNav.h"

@implementation UIViewController (JJBaseNav)
static char textFieldDidEndEditingKey;
-(void)setTextFieldDidEndEditingBlock:(TextFieldDidEndEditingBlock)textFieldDidEndEditingBlock{
    objc_setAssociatedObject(self, &textFieldDidEndEditingKey, textFieldDidEndEditingBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(TextFieldDidEndEditingBlock)textFieldDidEndEditingBlock{
    return (TextFieldDidEndEditingBlock)objc_getAssociatedObject(self, &textFieldDidEndEditingKey);
}

static char textFDidEditingBlockKey;
-(void)setTextFDidEditingBlock:(TextFDidEditingBlock)textFDidEditingBlock{
    objc_setAssociatedObject(self, &textFDidEditingBlockKey, textFDidEditingBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(TextFDidEditingBlock)textFDidEditingBlock{
    return (TextFDidEditingBlock)objc_getAssociatedObject(self, &textFDidEditingBlockKey);
}

static char searchTextFkKey;
-(void)setSearchTextF:(UITextField *)searchTextF{
    objc_setAssociatedObject(self, &searchTextFkKey, searchTextF, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UITextField *)searchTextF{
    return (UITextField *)objc_getAssociatedObject(self, &searchTextFkKey);
}



/// 显示导航栏
-(void)jj_showNavi{
    self.navigationController.navigationBar.hidden = NO;
}

/// 隐藏导航栏
-(void)jj_hiddenNavi{
    self.navigationController.navigationBar.hidden = YES;
}

/// 状态栏不隐藏
-(void)jj_showStatusBar{
    [UIApplication sharedApplication].statusBarHidden = NO;
}

#pragma mark 默认
/// 白底无分界线
-(void)jj_navDefault{
    [self jj_showNavi];
    [self jj_navPureWrite];
    [self jj_showStatusBar];
}

-(void)jj_FMNav{
    self.edgesForExtendedLayout = 0;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor}];
    self.hidesBottomBarWhenPushed = YES;
}

#pragma mark 白色底
/// 白底带分界线
-(void)jj_navWriteWithLine{
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.barTintColor=nil;
    self.navigationController.navigationBar.tintColor=kBlackColor;
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName :kBlackColor,
                                   NSFontAttributeName:kBoldFont(17)};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    [[UINavigationBar appearance] setTintColor:kRGBA(255, 255, 255, 1)];
    self.navigationController.navigationBar.translucent=NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    [self jj_navLine:NO];
    [self defLeftBarImage:@"icon_back_black"];
}

/// 白底无分界线
-(void)jj_navPureWrite{
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.barTintColor=nil;
    self.navigationController.navigationBar.tintColor= kBlackColor;
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName:kBlackColor,
                                   NSFontAttributeName:kBoldFont(17)};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    
    [[UINavigationBar appearance] setTintColor:kBlackColor];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.translucent=NO;
    [self defLeftBarImage:@"icon_back_black"];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}


/// 灰底带搜索栏&分界线
-(void)jj_navGrayWithLineAndSearchBar:(UIKeyboardType)keyboardType{
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.barTintColor= kHexColor(0xF7F7F7);
    self.navigationController.navigationBar.tintColor=kBlackColor;
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName :kBlackColor,
                                   NSFontAttributeName:kBoldFont(17)};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    [[UINavigationBar appearance] setTintColor:kHexColor(0xF7F7F7)];
    self.navigationController.navigationBar.translucent=NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
    [self jj_navLine:NO];
    [self defLeftBarImage:@"icon_back_black"];
    
    CGRect mainViewBounds = self.navigationController.view.bounds;
    self.searchTextF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+KStatusBarH + 8, CGRectGetWidth(mainViewBounds)-120, 30)];
    self.searchTextF.placeholder = @"搜索";
    self.searchTextF.delegate = self;
    self.searchTextF.font = kMediumFont(14);
    self.searchTextF.textColor = kBlackColor;
    self.searchTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTextF.backgroundColor = [UIColor clearColor];
    self.searchTextF.returnKeyType = UIReturnKeySearch;
    self.searchTextF.keyboardType = keyboardType;
    self.searchTextF.tag = 99;
    
    
    for (UIView *v in self.navigationController.view.subviews) {
        if (v.tag == 99) {
            [v removeFromSuperview];
        }
    }
    [self.navigationController.view addSubview:self.searchTextF];
}


#pragma mark 自定义
-(void)jj_navCustom{
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.barTintColor=kMainColor;
    self.navigationController.navigationBar.tintColor=kBgColor;
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName :kRGBA(255, 255, 255, 1),
                                   NSFontAttributeName:kBoldFont(17)};
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    self.navigationController.navigationBar.translucent=NO;
    [self jj_navLine:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
#pragma mark 黑色底
-(void)jj_navBlack{
    
    
    [self.navigationController.navigationBar lt_reset];
    
    self.navigationController.navigationBar.barTintColor = kRGBA(0, 0, 0, 1);
    self.navigationController.navigationBar.tintColor = kRGBA(255, 255, 255, 1);
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName :kRGBA(255, 255, 255, 1),
                                   NSFontAttributeName:kBoldFont(18)};
    
    [[UINavigationBar appearance] setTintColor:kRGBA(255, 255, 255, 1)];
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    self.navigationController.navigationBar.translucent=YES;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self jj_navLine:NO];
}
#pragma mark 透明
-(void)jj_navTransparent{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    NSDictionary *textAttributes=@{NSForegroundColorAttributeName :kRGBA(255, 255, 255, 1),
                                   NSFontAttributeName:kFont(20)};
    
    [[UINavigationBar appearance] setTintColor:kRGBA(255, 255, 255, 1)];
    self.navigationController.navigationBar.titleTextAttributes=textAttributes;
    self.navigationController.navigationBar.translucent=YES;
    [self jj_navLine:YES];
    [self defLeftBarImage:@"icon_back_black"];
}

#pragma mark 去除透明
-(void)jj_navRemoveTransparent{
 
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self jj_navLine:NO];
    
}

-(void)jj_navLine:(BOOL)hidden{
    
    //iOS10及以上的隐藏方法
    if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
            NSArray *list=self.navigationController.navigationBar.subviews;
            for (id obj in list) {
                //10.0的系统字段不一样
                UIView *view =   (UIView*)obj;
                for (id obj2 in view.subviews) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *image =  (UIImageView*)obj2;
                        if (image.frame.size.height <= 1) {
                            image.hidden = hidden;
                        }
                    }
                    
                  
                }
            }
            
        }
        
        return;
    }
    //iOS10以下的隐藏方法
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)obj;
                NSArray *list2 = imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2 = (UIImageView *)obj2;
                        if (imageView2.frame.size.height <= 1) {
                            imageView2.hidden = hidden;
                        }
                    }
                    if ([obj2 isKindOfClass:[UIView class]]) {
                        UIView *view2 = (UIView *)obj2;
                        if (view2.frame.size.height <= 1) {
                            view2.hidden = hidden;
                        }
                    }
                }
            }
        }
    }
    
    
}


#pragma mark- UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    !self.textFieldDidEndEditingBlock?:self.textFieldDidEndEditingBlock(textField);
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
    }
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    !self.textFDidEditingBlock?:self.textFDidEditingBlock(toBeString);
    return YES;
}


@end
